package com.qdu.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qdu.entity.Collections;
import com.qdu.entity.Cotenancy;
import com.qdu.entity.Users;
import com.qdu.service.CollectionService;
import com.qdu.service.CotenancyService;
import com.qdu.service.CotenancymessageService;
import com.qdu.service.HomemessageService;

@Controller
public class CotenancyController {
    @Autowired
    private CotenancyService cotenancyservice;
    @Autowired
    private CotenancymessageService  mecotenancyservice;
    
    @Autowired
    private CollectionService collect2;
    
    @Autowired
    private HomemessageService homeService;
	
    @GetMapping("/cotenancy/{Uid}")
    public String tocotenancy(@PathVariable String Uid,Model model) {
    	model.addAttribute("cotelist",cotenancyservice.getAllCotenancy());
    	return "Cotenancy";
    }
    @GetMapping("/cotenancy/mycotenancy/{Uid}")
    public String tomycotenancy(@PathVariable String Uid,Model model) {
    	model.addAttribute("cotelist",cotenancyservice.getAllCotenancyByUid(Uid));
    	return "mycotenancy";
    }
    
    @PostMapping("/cotenancy/collect")
    @ResponseBody
    public void collect(String Uid1,String Uid2,String Hid) {
        Collections collect=new Collections(Uid1,Uid2,Hid);
        collect2.addCollection(collect);
    }
    
    @GetMapping("/cotenancy/cotenancyinfo/{uid}/{hid}")
    public String houseinfoother(@PathVariable String uid,@PathVariable String hid,Model model) {
    	model.addAttribute("cotenancy",cotenancyservice.getOneCotenancyByUid(uid, hid));
    	model.addAttribute("comment",mecotenancyservice.getAllCotenancymessageByUid(uid, hid));
    	return "cotenancyInfo2";
    }
    

    
    @PostMapping("/cotenancy/edit_cotenancy")
    public String edituser(Cotenancy cotenancy,Model model,HttpSession session) {
    	Users user=(Users)session.getAttribute("sessionuser");
    	if(user.getUid().equals(cotenancy.getUid())){
    		cotenancy.setCprice(4);
    		
    		return "index";
    	
}
    	else {
		return "powerless";
    	}
}
    
    
    @GetMapping("/cotenancy/publichcotenancy/{Uid}")
    public String publichcotenancy(@PathVariable String Uid,Model model) {
    	model.addAttribute("Uid",Uid);
    	return "publishcotenancy";
    	
    }
    
    @PostMapping("/publishcotenancy")
    public String publishhouse(Cotenancy cotenancy,Model model,HttpSession session) {
    	cotenancyservice.addCotenancy(cotenancy);
    	
		model.addAttribute("cotelist",cotenancyservice.getAllCotenancy());
    	return "Cotenancy";
    }
}